/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
	January 7th, 2021

	DESCRIPTION: Estimates the SD of Temporary and Regular Work Arrangement FE
    that are presented in the text.

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/13_SD_Calculations`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/AKM_Firm_FE_for_export_stata.dta", clear

/****************************************************************************************
* Run SD calculations
****************************************************************************************/

gen total_obs = obs

winsor2 firm_fe4, replace cuts(1 99) trim
winsor2 uf_fe4, replace cuts(1 99) trim

* Calculate raw and measurement-error-corrected variance and SD
gen user_firm_sample = (firm_fe4 != . & connected_set == 1 & uf_fe4 != .)

sum firm_fe4 if user_firm_sample==1 [aw=total_obs]
global regular_firm_fe_sd =  r(sd)
sum uf_fe4 if user_firm_sample==1 [aw=total_obs]
global user_firm_fe_sd =  r(sd)

winsor2 firm_fe4_g0, replace cuts(1 99) trim
winsor2 firm_fe4_g1, replace cuts(1 99) trim
winsor2 uf_fe4_g0, replace cuts(1 99) trim
winsor2 uf_fe4_g1, replace cuts(1 99) trim

* we are interested in cov(firm_fe4_g0,firm_fe4_g1)	and the analogous expression for user firm effects
* we have that b = cov(x,y)/var(x,x) so that cov(x,y) = b*var(x,x) so that sqrt(cov(x,y)) = sqrt(b*var(x,x))=sqrt(b)*sqrt(var(x,x))
* importantly, the term sqrt(b) is the relevant scaling factor for the standard deviation

reg firm_fe4_g0	firm_fe4_g1 if user_firm_sample [aw=total_obs]
sum firm_fe4_g1 if e(sample) [aw=total_obs]
global regular_firm_fe_50pct_sd = r(sd)
global sqrt_b = (_b[firm_fe4_g1])^0.5
global cor_regular_firm_fe_50pct_sd = ${sqrt_b}*${regular_firm_fe_50pct_sd}
global cor_regular_firm_fe_sd = ${sqrt_b}*${regular_firm_fe_sd}

display("Raw SD of regular firm effects in sample: ${regular_firm_fe_sd}")
display("Raw SD of regular firm effects in 50% sample: ${regular_firm_fe_50pct_sd}")
display("Measurement-error-corrected SD of regular firm effects is ${sqrt_b} of raw SD")
display("Measurement-error-corrected SD of regular firm effects in total sample: ${cor_regular_firm_fe_sd}")
display("Measurement-error-corrected SD of regular firm effects in 50% sample: ${cor_regular_firm_fe_50pct_sd}")

reg uf_fe4_g0	uf_fe4_g1 if user_firm_sample [aw=total_obs]
sum uf_fe4_g1 if e(sample) [aw=total_obs]
global user_firm_fe_50pct_sd = r(sd)
global sqrt_b = (_b[uf_fe4_g1])^0.5
global cor_user_firm_fe_50pct_sd = ${sqrt_b}*${user_firm_fe_50pct_sd}
global cor_user_firm_fe_sd = ${sqrt_b}*${user_firm_fe_sd}

display("Raw SD of user firm effects in sample: ${user_firm_fe_sd}")
display("Raw SD of user firm effects in 50% sample: ${user_firm_fe_50pct_sd}")
display("Measurement-error-corrected SD of user firm effects is ${sqrt_b} of raw SD")
display("Measurement-error-corrected SD of user firm effects in total sample: ${cor_user_firm_fe_sd}")
display("Measurement-error-corrected SD of user firm effects in 50% sample: ${cor_user_firm_fe_50pct_sd}")

log close
